package cn.imvc.app.models;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Department;
import cn.imvc.app.entities.Department_position;
import cn.imvc.app.entities.Position;
import cn.imvc.app.repositories.DepartmentPositionRepository;
import cn.imvc.app.repositories.DepartmentRepository;
import cn.imvc.app.repositories.PositionRepository;
import cn.imvc.core.HttpContext;
@Service
public class PositionDepartmentModelImpl implements PositionDepartmentModel {

	@Autowired
	private PositionRepository positionRepository;
	@Autowired
	private DepartmentRepository departmentRepository;
	@Autowired
	private DepartmentPositionRepository departmentPositionRepository;
	
	public Set<Department_position> get(int did ,int pid){

		return departmentPositionRepository.findByDepartmentAndPosition(departmentRepository.findOne(did),positionRepository.findOne(pid));
	}
	
	public String getPositions() {
		List<Position> positionlist=positionRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer positions=new StringBuffer();
		for (Position position : positionlist) {
			positions.append("<li><a href='"+HttpContext.getRootUrl()+"/system/position-department/"+position.getId()+"' target='ajax' rel='departmentTree'>"+position.getName()+"</a></li>");
		}
		return positions.toString();
	}

	public String getDepartmentTree(int positionId) {
		List<Department> departments = departmentRepository.findByParentIsNull(new Sort(Direction.DESC,"order"));
		StringBuffer tree = new StringBuffer();
		String checked="";
		Position position=positionRepository.findOne(positionId);
		for (Department department : departments) {
			checked="";
			for (Object object : department.getDepartment_position()) {
				Department_position department_position=(Department_position)object;
				if(department_position.getPosition().equals(position)){
					checked="checked='true'";
				}
			}
			tree.append("<li><a tname='departmentid' tvalue='"+department.getId()+"' "+checked+" >"+department.getName()+"</a>");
			tree.append(getChildDepartment(department,position));
			tree.append("</li>");

		}
		return tree.toString();
	}
		

	
	private String getChildDepartment(Department department,Position position) {
		StringBuffer childNode = new StringBuffer();

		if (department.getDepartment().size() > 0) {
			childNode.append("<ul>");
			String checked="";
			for (Object Object : department.getDepartment()) {
				Department childDepartment = (Department) Object;
				checked="";
				for (Object object : childDepartment.getDepartment_position()) {
					Department_position department_position=(Department_position)object;
					if(department_position.getPosition().equals(position)){
						checked="checked='true'";
					}
				}
				childNode.append("<li><a tname='departmentid' tvalue='"+childDepartment.getId()+"' "+checked+">"+childDepartment.getName()+"</a>");
				childNode.append(getChildDepartment(childDepartment,position));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}

		return childNode.toString();
		
	}
	
	public void savePositionDepartment(int id, Set<Department> departments) {
		Position position=positionRepository.findOne(id);
		for (Object object : position.getDepartment_position()) {
			Department_position department_position=(Department_position)object;
			departmentPositionRepository.delete(department_position);
		}
		
		for (Department department : departments) {
			Department_position department_position=new Department_position();
			department_position.setPosition(position);
			department_position.setDepartment(department);
			departmentPositionRepository.saveAndFlush(department_position);
		}
	}

	public String getDepartments() {
		List<Department> departmentlist=departmentRepository.findByParentIsNull(new Sort(Direction.DESC,"order"));
		StringBuffer departments=new StringBuffer();
		for (Department department : departmentlist) {
			departments.append("<li><a href='"+HttpContext.getRootUrl()+"/system/department-position/"+department.getId()+"' target='ajax' rel='positionTree'>"+department.getName()+"</a>");
			departments.append(getChildDepartment(department));
			departments.append("</li>");
		}
		return departments.toString();
	}

	private String getChildDepartment(Department department){
		StringBuffer childNode = new StringBuffer();
		if (department.getDepartment().size() > 0) {
			childNode.append("<ul>");
			for (Object Object : department.getDepartment()) {
				Department childDepartment = (Department) Object;
				childNode.append("<li><a href='"+HttpContext.getRootUrl()+"/system/department-position/"+childDepartment.getId()+"' target='ajax' rel='positionTree'>"+childDepartment.getName()+"</a></li>");
				childNode.append(getChildDepartment(childDepartment));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}

		return childNode.toString();
	}
	
	
	
	
	
	public String getPositionTree(int departmentId) {
		List<Position> positions = positionRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer tree = new StringBuffer();
		String checked="";
		Department department=departmentRepository.findOne(departmentId);
		for (Position position : positions) {
			checked="";
			for (Object object : department.getDepartment_position()) {
				Department_position department_position=(Department_position)object;
				if(department_position.getPosition().equals(position)){
					checked="checked='true'";
				}
			}
			tree.append("<li><a tname='positionid' tvalue='"+position.getId()+"' "+checked+" >"+position.getName()+"</a></li>");
		}
		return tree.toString();
	}

	public void saveDepartmentPosition(int id, Set<Position> positions) {
		Department department=departmentRepository.findOne(id);
		for (Object object : department.getDepartment_position()) {
			Department_position department_position=(Department_position)object;
			departmentPositionRepository.delete(department_position);
		}
		
		for (Position position : positions) {
			Department_position department_position=new Department_position();
			department_position.setPosition(position);
			department_position.setDepartment(department);
			departmentPositionRepository.saveAndFlush(department_position);
		}
		
	}

	public String getPositions(int departmentId) {

		Department department=departmentRepository.findOne(departmentId);
		StringBuffer str=new StringBuffer();
		str.append("[");
		if(department!=null&&department.getDepartment_position()!=null&&department.getDepartment_position().size()>0){
		for (Object object : department.getDepartment_position()) {
			Department_position department_position=(Department_position)object;
			str.append("[\""+department_position.getPosition().getId()+"\", \""+department_position.getPosition().getName()+"\"],");
		}
			str.replace(str.length()-1,str.length(), "]");
		}else{
			str.append("]");
		}
		
		return str.toString();
	}

}
